package com.leetcode.No0901;

import org.junit.Test;

/**
 * @program: Solution
 * @description: 股票价格跨度
 * @author: Wang Zhihua
 * @date: 2022-10-21
 * Your StockSpanner object will be instantiated and called as such:
 * StockSpanner obj = new StockSpanner();
 * int param_1 = obj.next(price);
 */
public class StockSpanner {

    private Node last;

    public StockSpanner() {
        last = new Node();
    }

    public int next(int price) {
        last.price = price;

        Node curNode = new Node();
        curNode.pre = last;
        last = curNode;

        int ans = 1;
        curNode = curNode.pre;
        while ((curNode = curNode.pre) != null && curNode.price <= price) {
            ++ans;
        }
        System.out.println(ans);
        return ans;
    }

    private class Node {
        int price;

        Node pre = null;
    }

    @Test
    public void test01() {
        StockSpanner stockSpanner = new StockSpanner();
        stockSpanner.next(100); // 返回 1
        stockSpanner.next(80);  // 返回 1
        stockSpanner.next(60);  // 返回 1
        stockSpanner.next(70);  // 返回 2
        stockSpanner.next(60);  // 返回 1
        stockSpanner.next(75);  // 返回 4 ，因为截至今天的最后 4 个股价 (包括今天的股价 75) 都小于或等于今天的股价。
        stockSpanner.next(85);  // 返回 6
    }
}
